package com.jake.sort.map;

import java.util.*;
import java.util.stream.Collectors;

public class MapSort {
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "ZK");
        map.put("age", 13);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "ZA");
        map2.put("age", 15);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("name", "CX");
        map3.put("age", 20);

        Map<String, Object> map4 = new HashMap<>();
        map4.put("name", "CX");
        map4.put("age", 18);

        List<Map<String, Object>> list = new ArrayList<>();
        list.add(map);
        list.add(map2);
        list.add(map3);
        list.add(map4);

        System.out.println("排序前---" + list);

        // 排序代码如下
        List<Map<String, Object>> collect = list.stream().sorted(Comparator.comparing(MapSort::comparingByName)
                .thenComparing(Comparator.comparing(MapSort::comparingByAge).reversed()))
                .collect(Collectors.toList());

        list.sort(Comparator.comparing(MapSort::comparingByName).thenComparing(Comparator.comparing(MapSort::comparingByAge).reversed()));

        System.out.println("排序后---" + collect);
        System.out.println("排序后---" + list);
    }

    private static String comparingByName(Map<String, Object> map) {
        return String.valueOf(map.get("name"));
    }

    private static Integer comparingByAge(Map<String, Object> map) {
        return (Integer) map.get("age");
    }
}